package com.xinqi.util;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author xin麒
 * @date 2023/8/18 18:23
 */

public class ThreadPoolUtils {
    public static void shutdownThreadPool(ThreadPoolExecutor executor) {
        if (executor != null) {
            executor.shutdown();
            executor = null;
        }
    }

    public static ThreadPoolExecutor getThreadPoolWithAbortPolicy(int corePoolSize,
                                                   int maximumPoolSize) {
        return new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                0,
                TimeUnit.SECONDS,
                new SynchronousQueue<>(),
                new ThreadPoolExecutor.AbortPolicy()
        );
    }

    public static ThreadPoolExecutor getThreadPoolWithCallerRunsPolicy(int corePoolSize,
                                                   int maximumPoolSize) {
        return new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                0,
                TimeUnit.SECONDS,
                new SynchronousQueue<>(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
    }
}
